module.exports = function ($scope, $state, $resource, $modal, str2date, date2str, $stateParams,
	saleup, saledown, saleupdate, talist, sellerListno, sellerList, tstcreateno, tstcreate, tststartno, tststart, tststopno, tststop, toaster
) {
	$scope.searchform = {'moreSearch': false};
	$scope.dictbytypelist = [];
	$scope.moreSearch = function(){
		$scope.searchform.moreSearch = !$scope.searchform.moreSearch;
	}
	$scope.bigCurrentPage = 1;
	$scope.itemsPerPage = 10;
	$scope.statusArr = [
		{name: '--全部--', value: ''},
		{name: '草稿', value: '0'},
		{name: '上架', value: '1'},
		{name: '下架', value: '2'}
	];

	$resource('/api/ac/tc/officeBindService/getAllList', {}, {}).save({'tag': '2'}, function (res) {
		if (res.errcode === 0) {
			$scope.channel_list = res.data.channelList;
			$scope.channel_list.unshift({ name: '--全部渠道--', code: '' });
		} else {
			toaster.error({ title: "", body: res.errmsg })
		}
	});

	if ($stateParams.sale_type == 'H') {
		$resource('/api/as/sc/dict/dictbytypelist', {}, {})
		.save({'type': 'sale_category', 'class_type': 'H'}, function (res) {
			if (res.errcode !== 0) {
				toaster.error({ title: "提示", body: res.errmsg });
				return;
			}
			$scope.dictbytypelist = res.data;
			$scope.dictbytypelist.splice(0, 0, {label: '--全部--', value: ''})
		});
		$resource('/api/as/tc/placeview/hlist', {}, {}).save({type: 'H'}, function (res) {
			if (res.errcode === 0) {
				$scope.viewarr = res.data;
				$scope.viewarr.unshift({ name: '--全部场所--', code: '' });
			} else {
				toaster.error({ title: "", body: res.data.errmsg })
			}
		});
	} else {
		$resource('/api/as/sc/dict/getSaleCategoryList', {}, {}).save({}, function (res) {
			if (res.errcode !== 0) {
				toaster.error({ title: "提示", body: res.errmsg });
				return;
			}
			$scope.dictbytypelist = res.data;
			$scope.dictbytypelist.splice(0, 0, {label: '--全部--', value: ''})
		});
		$resource('/api/as/tc/placeview/jlist', {}, {}).save({'type': 'D'}, function (res) {
			if (res.errcode === 0) {
				$scope.viewarr = res.data;
				$scope.viewarr.unshift({ name: '--全部场所--', code: '' });
			} else {
				toaster.error({ title: "", body: res.data.errmsg })
			}
		});
	}

	$scope.load = function () {
		var para = {
			pageNo: $scope.bigCurrentPage,
			pageSize: $scope.itemsPerPage,
		};
		if($stateParams.sale_type == 'H') {
			para.sale_type = 'H'
		}
		angular.extend(para, $scope.searchform);
		$resource('/api/ac/tc/ticketSaleService/getSaleList', {}, {}).save(para, function(res){
			if (res.errcode !== 0) {
				toaster.error({ title: "提示", body: res.errmsg });
				return;
			}
			$scope.objs = res.data.results;
			for (var index = 0; index < $scope.objs.length; index++) {
				switch ($scope.objs[index].sale_apply_state) {
					case 'new':
						$scope.objs[index].sale_apply_state_name = '';
						break;
					case 'apply_sale':
						$scope.objs[index].sale_apply_state_name = '申请上架';
						break;
					case 'wait_sale':
						$scope.objs[index].sale_apply_state_name = '审核通过等待上架';
						break;
					case 'for_sale':
						$scope.objs[index].sale_apply_state_name = '销售中';
						break;
					case 'pause':
						$scope.objs[index].sale_apply_state_name = '暂停销售';
						break;
					case 'sale_end':
						$scope.objs[index].sale_apply_state_name = '下架';
						break;
					case 'ban_sale':
						$scope.objs[index].sale_apply_state_name = '禁止销售';
						break;
					default:
						$scope.objs[index].sale_apply_state_name = '';
						break;
				}
				if($scope.objs[index].channel_list_name && typeof $scope.objs[index].channel_list_name == 'string') {
					$scope.objs[index].channel_list_name = $scope.objs[index].channel_list_name.replace(/,/g, '，');
				}
			}
			$scope.bigTotalItems = res.data.totalRecord;
		});
	};
	$scope.load();

	//申请上架
	$scope.start = function(id){
		saleup.get({'id': id}, function(res){
			if(res.errcode === 0){
				$scope.load();
			} else {
				toaster.error({title: "提示", body: res.errmsg });
			}
		});
	}
	//下架
	$scope.stop = function(id){
		saledown.get({'id': id}, function(res){
			if(res.errcode === 0){
				$scope.load();
			} else {
				toaster.error({ title: "提示", body: res.errmsg });
			}
		});
	}

	$scope.edit = function(obj){
		$resource('/api/as/sc/dict/getSaleCategoryList', {}, {})
			.save({ 'type': 'sale_category' }, function (res) {
				if (res.errcode !== 0) {
					toaster.error({ title: "", body: res.errmsg });
					return false;
				}

				var flag = false;
				for (var index = 0; index < res.data.length; index++) {
					var element = res.data[index];
					if (element.value == obj.sale_category) {
						flag = true;
					}
				}
				if (!flag) {
					toaster.error({ title: "您的账号无权查看该类型销售品", body: "请配置销售品分类权限" });
					return false;
				}
				//2019年01月04日11:49:22 产品编辑页由弹出页变为页面跳转
				$state.go('app.editProduct', {'id':obj.id,'obj': obj.name});
				// var modalInstance = $modal.open({
				// 	template: require('../views/product.html'),
				// 	controller: 'editProduct',
				// 	url: '/product/edit/:id',
				// 	size: 'lg',
				// 	resolve: {
				// 		'productid': function () {
				// 			return obj.id;
				// 		},
				// 		'what': function () {
				// 			return 'edit';
				// 		},
				// 		'str2date': function () {
				// 			return str2date1;
				// 		},
				// 		'date2str': function () {
				// 			return date2str1;
				// 		},
				// 		'auditing': function () {
				// 			return false;
				// 		}
				// 	}
				// });

				// modalInstance.result.then(function (showResult) {
				// 	$scope.load();
				// }, function (reason) {
				// 	$scope.load();
				// });

			});
	};

	$scope.asort = function(id, asort){
		saleupdate.save({ 'id': id, 'asort': asort }, function (res) {
			if (res.errcode === 0) {
				$scope.load();
			} else {
				toaster.error({ title: "提示", body: res.errmsg });
			}
		});
	};


	$scope.getinfo = function (obj) {
		var flag = false;
		for (var index = 1; index < $scope.dictbytypelist.length; index++) {
			var element = $scope.dictbytypelist[index];
			if (element.value == obj.sale_category) {
				flag = true;
			}
		}
		if (!flag) {
			toaster.error({ title: "您的账号无权查看该类型销售品", body: "请配置销售品权限" });
			return false;
		}
		var str2date1 = str2date;
		var date2str1 = date2str;
		var modalInstance = $modal.open({
			template: require('../views/productInfo.html'),
			controller: 'infoProduct',
			url: '/infoProduct/:id',
			size: 'lg',
			resolve: {
				'productid': function () {
					return obj.id;
				},
				'str2date': function () {
					return str2date1;
				},
				'date2str': function () {
					return date2str1;
				},
				'auditing': function () {
					return false;
				}
			}
		});
	};



	//flag:1,分配经销商
	//flga:0,不允许销售
	$scope.distribution = function (code, flag) {

		var modalInstance;

		var talist1 = talist;
		var sellerList1 = sellerList;
		var tstcreate1 = tstcreate;
		var tststart1 = tststart;
		var tststop1 = tststop;


		if (flag == 1) {
			modalInstance = $modal.open({
				template: require('../views/distribution.html'),
				controller: 'distribution',
				//size: 'lg',
				resolve: {
					'code': function () {
						return code;
					},
					'talist': function () {
						return talist1;
					},
					'sellerList': function () {
						return sellerList1;
					},
					'tstcreate': function () {
						return tstcreate1;
					},
					'tststart': function () {
						return tststart1;
					},
					'tststop': function () {
						return tststop1;
					},
					'title': function () {
						return '分配经销商';
					}
				}
			});
		}
		else if (flag == 0) {
			modalInstance = $modal.open({
				template: require('../views/nodistribution.html'),
				controller: 'nodistribution',
				//size: 'lg',
				resolve: {
					'code': function () {
						return code;
					},
					'talist': function () {
						return talist1;
					},
					'sellerList': function () {
						return sellerListno1;
					},
					'tstcreate': function () {
						return tstcreateno1;
					},
					'tststart': function () {
						return tststartno1;
					},
					'tststop': function () {
						return tststopno1;
					},
					'title': function () {
						return '分配不允许销售经销商';
					}
				}
			});
		}


		modalInstance.opened.then(function () {// 模态窗口打开之后执行的函数  
			// $scope.load();
		});
		modalInstance.result.then(function (showResult) {
			$scope.load();
		}, function (reason) {
			$scope.load();
			// // click，点击取消，则会暑促cancel  
			// $log.info('Modal dismissed at: ' + new Date());
		});

	};

	$scope.del = function (id) {

		if (confirm("您确认要删除吗？")) {

			saleupdate.save({ 'id': id, 'del_flg': '1' }, function (res) {

				if (res.errcode === 0) {
					toaster.success({ title: "", body: '删除成功' });
					$scope.load();
				}
				else {
					toaster.error({ title: "提示", body: res.errmsg });
				}

			});

		}
	};
	$scope.skset = function (id, code, name, guide_price, cost_price, productflag) {
		$scope.item = '';
		productflag = 'productflag';
		var modalInstance = $modal.open({
			template: require('../views/skprofit.html'),
			controller: 'skprofit',
			url: '/skprofit',
			size: 'lg',
			resolve: {
				'item': function () {
					return $scope.item;
				},
				'id': function () {
					return id;
				},
				'code': function () {
					return code;
				},
				'name': function () {
					return name;
				},
				'guide_price': function () {
					return guide_price;
				},
				'cost_price': function () {
					return cost_price;
				},
				'productflag': function () {
					return productflag;
				},
				'what': function () {
					return 'edit';
				},

			}
		});

		modalInstance.result.then(function () {
		}, function () {
		});
		// $state.go('app.skprofit');
		// $state.go('app.skprofit',{'code' : code, 'name' : name, 'guide_price' : guide_price, 'cost_price' : cost_price});
	}

	$scope.offSale = function (id) {
		$resource('/api/as/tc/sale/offSale', {}, {})
			.save({ id: id }, function (res) {
				if (res.errcode !== 0) {
					toaster.error({ title: "提示", body: res.errmsg });
					return;
				}
				toaster.success({ title: "提示", body: '修改成功!' });
				$scope.load();
			});
	}

	$scope.onSale = function (id) {
		$resource('/api/as/tc/sale/onSale', {}, {})
			.save({ id: id }, function (res) {
				if (res.errcode !== 0) {
					toaster.error({ title: "提示", body: res.errmsg });
					return;
				}
				toaster.success({ title: "提示", body: '修改成功!' });
				$scope.load();
			});
	}


	//设置多图-微分销用
	// $scope.setImages = function (obj) {

	// 	$resource('/api/as/wc/productshare/getshareattr', {}, {}).save({ code: obj.code }, function (res) {

	// 		var pobj = {
	// 			'code': obj.code,
	// 			'title': obj.name,
	// 		};

	// 		var lala = {};

	// 		if (res.errcode === 0) {
	// 			//有数据
	// 			// pobj['top_pic'] = res.data.top_pic;
	// 			// pobj['words'] = res.data.words;
	// 			// pobj['bookingnotes'] = res.data.bookingnotes;
	// 			// pobj['detail'] = res.data.detail;

	// 			angular.extend(res.data, pobj);

	// 			lala = res.data;
	// 		}
	// 		else if (res.errcode === 10003) {
	// 			//没数据
	// 			angular.extend(lala, pobj);
	// 		}
	// 		else {
	// 			alert(res.errmsg);
	// 			return;
	// 		}

	// 		var modalInstance = $modal.open({
	// 			template: require('../../51weshop/views/settoppic.html'),
	// 			controller: 'weshopsettoppic',
	// 			url: '/weshopsettoppic',
	// 			size: 'lg',
	// 			resolve: {
	// 				'product': function () {
	// 					return lala;
	// 				},
	// 				str2date: function () {
	// 					return str2date;
	// 				},
	// 				date2str: function () {
	// 					return date2str;
	// 				},

	// 			}
	// 		});

	// 		modalInstance.opened.then(function () {// 模态窗口打开之后执行的函数  
	// 			// $scope.load();
	// 		});
	// 		modalInstance.result.then(function (showResult) {
	// 			$scope.load();
	// 		}, function (reason) {
	// 			$scope.load();
	// 			// // click，点击取消，则会暑促cancel  
	// 			// $log.info('Modal dismissed at: ' + new Date());
	// 		});

	// 	});

	// };



	//配置海报
	// $scope.setPoster = function (obj) {

	// 	$resource('/api/ac/wc/productShareService/getshareattr', {}, {}).save({ code: obj.code }, function (res) {

	// 		var pobj = {
	// 			'code': obj.code,
	// 			'title': obj.name,
	// 		};

	// 		if (res.errcode === 0) {
	// 			//没有数据
	// 			if (res.data.xxx || res.data.xxx == 'yyy') {

	// 			}
	// 			//有数据
	// 			else {
	// 				pobj['id'] = obj.id;
	// 				if (res.data.img) {
	// 					pobj['share_pic'] = res.data.img;
	// 				}
	// 				if (res.data.qrcode_size) {
	// 					pobj['qrcode_size'] = res.data.qrcode_size;
	// 				}
	// 				if (res.data.width) {
	// 					pobj['width'] = res.data.width;
	// 				}
	// 				if (res.data.height) {
	// 					pobj['height'] = res.data.height;
	// 				}
	// 				if (res.data.nickname_width) {
	// 					pobj['nickname_width'] = res.data.nickname_width;
	// 				}
	// 				if (res.data.nickname_height) {
	// 					pobj['nickname_height'] = res.data.nickname_height;
	// 				}
	// 				if (res.data.nickname_size) {
	// 					pobj['nickname_size'] = res.data.nickname_size;
	// 				}
	// 				if (res.data.nickname_color) {
	// 					pobj['nickname_color'] = res.data.nickname_color;
	// 				}
	// 				if (res.data.nickname_length) {
	// 					pobj['nickname_length'] = res.data.nickname_length;
	// 				}
	// 			}
	// 		}
	// 		else if (res.errcode === 9999) {
	// 			//没数据

	// 		}
	// 		else {
	// 			alert(res.errmsg);
	// 			return;
	// 		}

	// 		var modalInstance = $modal.open({
	// 			template: require('../../51weshop/views/setposter.html'),
	// 			controller: 'weshopposter',
	// 			url: '/weshopposter',
	// 			size: 'lg',
	// 			resolve: {
	// 				'obj': function () {
	// 					return pobj;
	// 				},
	// 				str2date: function () {
	// 					return str2date;
	// 				},
	// 				date2str: function () {
	// 					return date2str;
	// 				}
	// 			}
	// 		});

	// 		modalInstance.opened.then(function () {// 模态窗口打开之后执行的函数  
	// 			// $scope.load();
	// 		});
	// 		modalInstance.result.then(function (showResult) {
	// 			$scope.load();
	// 		}, function (reason) {
	// 			$scope.load();
	// 			// // click，点击取消，则会暑促cancel  
	// 			// $log.info('Modal dismissed at: ' + new Date());
	// 		});


	// 		// var para = $state.get('app.weshopposter');

	// 		// var resolve = {
	// 		//          obj : function(){
	// 		//              return pobj;
	// 		//          },
	// 		//      };
	// 		//      angular.extend(para.resolve, resolve);

	// 		//      var modalInstance = $modal.open(para);
	// 		//      modalInstance.opened.then(function() {// 模态窗口打开之后执行的函数  
	// 		//      });  
	// 		//      modalInstance.result.then(function(result) {  
	// 		//          //load(); 

	// 		//      }, function(reason) {  
	// 		//          // click，点击取消，则会暑促cancel  
	// 		//          $log.info('Modal dismissed at: ' + new Date());  
	// 		//      }); 

	// 	});

	// };

	//设置产品是否接收微信消息（下单、退单）
	$scope.setReceiveMsg = function (obj){
		var msg = obj.receive_msg=='1'?'确认开此产品接收微信消息功能吗？':'关闭后，此产品相关的消息提醒停止发送！';
		if(confirm(msg)){
			var para = {
				'sale_code': obj.code,
				'receive_msg': obj.receive_msg
			};
			$resource('/api/as/tc/saleauth/setReceiveMsg', {}, {}).save(para, function(res){
				if (res.errcode === 0) {
					toaster.success({title: '', body: '成功'});
				} else {
					toaster.error({title: '', body: '失败'});
					obj.receive_msg = obj.receive_msg == '1'?'0':'1';
				}
			});
		} else {
			obj.receive_msg = obj.receive_msg == '1'?'0':'1';
		}
	}
    
	//设置公开范围，统一加价 2018年11月12日10:52:10
	$scope.change2 = function (obj, open_company){
		if(open_company == '0'){
			var modalInstance = $modal.open({
				template: require('../views/setUnifyPrice.html'),
				controller: 'setUnifyPrice',
				size: 'lg',
				resolve: {
					obj : function(){
						return obj;
					}
				}
			});
			//关闭模态框刷新页面
			modalInstance.result.then(function(result) {
				obj.open_company = result.open_company;
				obj.unify_float_price = result.unify_float_price
			}, function(reason){
				obj.open_company = '1';
			});
		} else {
			var para = {
				'sale_code': obj.code,
				'unify_float_price': '0',
				'open_company': '1'
			};
			if(confirm('修改后将删除已授权的分销商，确认修改为单独授权销售吗?')){
				$resource('/api/ac/tc/ticketSaleAuthService/updateOpen', {}, {}).save(para, function (res) {
					if (res.errcode === 0 && res.data.msg === 'success') {
						toaster.success({title: '', body: '成功'});
						$scope.load();
					} else {
						toaster.error({title: '', body: '失败'});
						obj.open_company = '0';
					}
				});
			} else {
				obj.open_company = '0';
			}
		}
	}

	$scope.setFxs = function (item) {
		item.negative = true;
		var modalInstance = $modal.open({
			template: require('../views/setFxs.html'),
			controller: 'setFxs',
			size: 'lg',
			resolve: {
				item : function () {
					return item;
				}
			}
		});
		//关闭模态框刷新页面
		modalInstance.result.then(function(result) {
			// $scope.load();
		});
	}
	//设置产品负责人
	$scope.setMaster = function () {
		$modal.open({
			template: require('../views/setMaster.html'),
			controller: 'setMaster',
			size: 'lg',
			resolve: {

			}
		});
	}
	
	//设置渠道
    $scope.setChannel = function (item){
        var modalInstance = $modal.open({
			template: require('../views/setChannel.html'),
			controller: 'setChannel',
			size: 'lg',
			resolve: {
				item : function () {
					return item;
				}
			}
		});
		//关闭模态框刷新页面
		modalInstance.result.then(function(result) {
			$scope.load();
		}, function(reason){
			$scope.load();
		});
	}
	
	//跳转我的渠道
	$scope.myChannel = function (item) {
		$state.go('app.myChannel', {});
	}

};